package com.baizhi.shane.sys.utils;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.dto.PageInDTO;
import com.baizhi.shane.shane.vo.PageInVO;
import com.baizhi.shane.sys.vo.SuccessedOutVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Component
public class CommonUtils {
    /**
     * 将数字转为布尔类型
     * countNum大于零，返回true，否则返回false
     *
     * @param countNum
     * @return
     */
    public boolean countToBoolean(Integer countNum) {
        boolean success = false;
        if (countNum > 0) {
            success = true;
        } else {
            success = false;
        }
        return success;
    }

    /**
     * 将数字转为字符串类型
     * countNum大于零，返回"操作成功"，否则返回"操作失败"
     *
     * @return java.lang.String
     * @Author 闻柏智
     * @Date 17:41 2023/4/30
     * @Param countNum
     **/
    public String countToString(Integer countNum) {
        String successString = "";
        if (countNum > 0) {
            successString = SysMapping.FAIL_STRING;
        } else {
            successString = SysMapping.SUCCESS_STRING;
        }
        return successString;
    }

    /**
     * 将布尔类型转换为字符串类型
     * success为true，返回"操作成功"，否则返回"操作失败"
     *
     * @return java.lang.String
     * @Author 闻柏智
     * @Date 17:43 2023/4/30
     * @Param success
     **/
    public String boolToString(boolean success) {
        String successString = "";
        if (success) {
            successString = SysMapping.SUCCESS_STRING;
        } else {
            successString = SysMapping.FAIL_STRING;
        }
        return successString;
    }

    /**
     * 将布尔类型转为字符串类型
     * 若成功输出successStr，否则输出failStr
     *
     * @Author 闻柏智
     * @Date 18:23 2023/10/4
     * @Param success
     * @Param successStr:
     * @Param failStr:
     * @return java.lang.String
     **/
    public String boolToString(boolean success, String successStr, String failStr) {
        String successString = "";
        if (success) {
            successString = successStr;
        } else {
            successString = failStr;
        }
        return successString;
    }

    /**
     * 将布尔类型转为SuccessedOutVO类型
     *
     * @Author 闻柏智
     * @Date 13:09 2023/12/31
     * @Param success
     * @Param successStr:
     * @Param failStr:
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    public SuccessedOutVO boolToSuccessedOutVO(boolean success, String successStr, String failStr) {
        if (StringUtils.isEmpty(successStr)) {
            successStr = SysMapping.SUCCESS_STRING;
        }
        if (StringUtils.isEmpty(failStr)) {
            failStr = SysMapping.FAIL_STRING;
        }
        SuccessedOutVO successedOutVO = new SuccessedOutVO();
        String message = this.boolToString(success, successStr, failStr);
        String successed = this.boolToString(success, SysMapping.NUMBER_1, SysMapping.NUMBER_0);
        successedOutVO.setMessage(message);
        successedOutVO.setSuccessed(successed);
        return successedOutVO;
    }

    /**
     * 将前端传入的pageNo转为SQL语句中的limit x,y 中的x
     *
     * @return int
     * @Author 闻柏智
     * @Date 20:15 2023/9/20
     * @Param pageNoFront
     * @Param pageSize:
     **/
    public int makePageNo(int pageNoFront, int pageSize) {
        if (pageNoFront < 1) {
            pageNoFront = 1;
        }
        int pageNo = (pageNoFront - 1) * pageSize;
        return pageNo;
    }

    /**
     * 将PageInVO的对象转换成PageInDTO的
     *
     * @Author 闻柏智
     * @Date 16:27 2023/9/24
     * @Param pageInVO
     * @Param userId:
     * @return com.baizhi.shane.shane.dto.PageInDTO
     **/
    public PageInDTO pageInVOToDTO(PageInVO pageInVO, Long userId) {
        PageInDTO pageInDTO = new PageInDTO();
        BeanUtils.copyProperties(pageInVO, pageInDTO);
        pageInDTO.setPageNo(this.makePageNo(pageInVO.getPageNo(), pageInVO.getPageSize()));
        pageInDTO.setUserId(userId);
        return pageInDTO;
    }


    public List<Long> idListStrToLongList(String idListStr) {
        // 先分解成字符串列表
        List<String> strList = Arrays.asList(idListStr.split(SysMapping.COMMA));
        /* 若为空直接报错 */
        if (CollectionUtils.isEmpty(strList)) {
            throw new RuntimeException("id列表字符串不能为空");
        }
        List<Long> idList = new ArrayList<>();
        for (String s : strList) {
            idList.add(Long.parseLong(s));
        }


        return idList;
    }
}